var QueryFeedOrder = function () {
  var template = /*html*/ `
    <div class="query-feed-order" v-loading.fullscreen.lock="loading||tableColumnKeyLoading">
      <div style="margin-top:10px;">
        <search-btn-view btn-text="投料单查询" @before-open="resetSearch" @closed="resetDropdown" @search="searchData">
          <template slot="right">
            <table-col-show-select v-if="tableHideColumnKey!==null" :table-name="tableName" :hide-column.sync="tableHideColumnKey"></table-col-show-select>
          </template>
          <div slot="search-content">
            <sys-common-search ref="searchRef" :search="search" request-url="materialSelectFilter/getMaterialSelectData" :request-data="{type:2}" 
              :search-field="['materialName','model','color','providerName','factoryNo','orderId','companyOrderId','customerNo']" label-width="135px" input-width="200px">
              <template slot="append">
                <el-form-item label="供方编号">
                  <el-input style="width:200px" v-model="search.providerNo" placeholder="请输入供方编号"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                  <el-select style="width:200px" v-model="search.status" placeholder="请选择状态">
                    <el-option value="-1" label="全部"></el-option>
                    <el-option v-for="status in statusList" :key="status.value" :label="status.label" :value="status.value"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="单据类型">
                  <el-select style="width:200px" v-model="search.type" placeholder="请选择单据类型">
                    <el-option value="" label="全部"></el-option>
                    <el-option v-for="type in typeList" :key="type.value" :label="type.label" :value="type.value"></el-option>
                  </el-select>
                </el-form-item>
              </template>
            </sys-common-search>
          </div>
        </search-btn-view>
      </div>
      <div class="feed-order-table-div">
        <el-table fit stripe border ref="feedOrder" :data="feedOrderList" style="width:100%;" height="parent" :hide-column-keys="getTableHideColumns()">
          <el-table-column :resizable="false" prop="companyOrderId" key="companyOrderId" label="关联单号" min-width="170px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" prop="factoryId" key="factoryId" label="工厂型号" :auto-width="true" min-width="70px"></el-table-column>
          <el-table-column :resizable="false" prop="providerNo" label="供方编号" :auto-width="true" min-width="70px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" prop="queue" label="订单序号" min-width="75px" align="center"></el-table-column>
          <el-table-column :resizable="false" prop="materialCode" key="materialCode" label="物料代码" :auto-width="true" min-width="70px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" prop="providerName" key="providerName" label="供应商" :auto-width="true" min-width="70px"></el-table-column>
          <el-table-column :resizable="false" prop="materialName" key="materialName" label="物料名称" :auto-width="true" min-width="70px"></el-table-column>
          <el-table-column :resizable="false" prop="color" key="color" label="编号" :auto-width="true" min-width="60px"></el-table-column>
          <el-table-column :resizable="false" prop="colorDescription" key="colorDescription" label="描述" :auto-width="true" min-width="70px"></el-table-column>
          <el-table-column :resizable="false" prop="model" key="model" label="规格型号" :auto-width="true" min-width="70px"></el-table-column>
          <el-table-column :resizable="false" prop="unit" key="unit" label="单位" min-width="80px" align="center" header-align="center"></el-table-column>
          <el-table-column :resizable="false" prop="materialType" key="materialType" label="物料类型" min-width="80px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" prop="feedCount" key="feedCount" label="计划投料数" min-width="100px"></el-table-column>
          <el-table-column :resizable="false" prop="productionUnitFeedCount" key="productionUnitFeedCount" label="块料占用数" min-width="90px"></el-table-column>
          <el-table-column :resizable="false" prop="count" key="count" label="仓库数量" min-width="80px"></el-table-column>
          <el-table-column :resizable="false" prop="notInCount" key="notInCount" label="未入库数量" min-width="100px"></el-table-column>
          <el-table-column :resizable="false" prop="relationCount" key="relationCount" label="关联数量" min-width="80px">
            <template slot-scope="scope">
              <div :class="{'table-btn':scope.row.relationCount>0}" @click="lookMaterialUseCount(scope.row)">{{scope.row.relationCount}}</div>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" prop="receiveCount" key="receiveCount" label="可领数量" min-width="80px"></el-table-column>
          <el-table-column :resizable="false" prop="purchaseCount" key="purchaseCount" label="应采购数量" min-width="100px"></el-table-column>
          <el-table-column :resizable="false" prop="createTime" key="createTime" label="制单时间" min-width="90px" align="center" header-align="center"></el-table-column>
          <el-table-column :resizable="false" prop="createUser" key="createUser" label="制单人" min-width="100px" align="center" header-align="center"></el-table-column>
          <el-table-column :resizable="false" key="status" label="单据状态" min-width="70px" align="center" header-align="center">
            <template slot-scope="scope">
              <span>{{getStatus(scope.row)}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" prop="approvalTime" key="approvalTime" label="审核时间" min-width="90px" align="center" header-align="center"></el-table-column>
          <el-table-column :resizable="false" prop="approvalUser" key="approvalUser" label="审核人" min-width="100px" align="center" header-align="center"></el-table-column>
          <el-table-column :resizable="false" key="operation" label="操作" fixed="right" :auto-width="true" min-width="50px" header-align="center">
            <template slot-scope="scope">
              <span class="table-btns">
                <template v-if="scope.row.hasApprovalOrder">
                  <template v-if="scope.row.status==='0'">
                  <el-button type="primary" size="mini" @click="approvalFeed(scope.row)">审核</el-button>
                  <el-button type="danger" size="mini" @click="retreatFeed(scope.row)">退回</el-button>
                  </template>
                  <template v-else-if="scope.row.status==='1'&&scope.row.isApperval">
                  <el-button type="primary" size="mini" @click="retreatFeed(scope.row)">退回</el-button>
                  </template>
                </template>
              </span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="query-feedorder-page" v-if="pagination.totalCount!==0">
        <el-pagination background :page-size="pagination.pageSize" :total="pagination.totalCount" :current-page.sync="pagination.currentPage"
          @current-change="pageChange"></el-pagination>
      </div>

      <el-dialog title="物料关联/占用数" :visible.sync="openMaterialUseCount" width="70%">
        <div style="height: 65vh;overflow: auto;padding:0 10px 10px;">
          <order-available-count v-if="openMaterialUseCount" :warehouse="materialData"></order-available-count>
        </div>
      </el-dialog>
    </div>
  `;

  return {
    mixins: [tableColShowMixin('feedOrder')],

    data: function () {
      var search = {
        status: '-1',
        type: '',
        warehouse: '',
        materialName: '',
        model: '',
        color: '',
        providerName: '',
        factoryNo: '',
        orderId: '',
        companyOrderId: '',
        customerNo: '',
        providerNo: ''
      };
      return {
        loading: false,
        feedOrderList: [],
        search: _.cloneDeep(search),
        oldSearch: _.cloneDeep(search),
        openMaterialUseCount: false,
        selectFeed: null,
        formDropdownOption: null,
        user: null,
        materialData: null,
        pagination: {
          currentPage: 1,
          totalCount: 0,
          pageSize: 10
        },
        statusList: [{
          label: '未审核',
          value: '0'
        }, {
          label: '已审核',
          value: '1'
        }],
        typeList: [{
          label: '生产投料单',
          value: '1'
        }, {
          label: '申补投料单',
          value: '2'
        }],
        unitList: []
      };
    },
    created: function () {
      this.setUnitList();
      var self = this;
      this.loading = true;
      SysUtil.getUser().then(function (user) {
        self.loading = false;
        self.user = user;
        self.setFeedOrderList(true);
      })
    },
    methods: {
      setUnitList: function () {
        var self = this;
        DropdownUtil.getUnitList().then(function (unitList) {
          self.unitList = unitList;
        });
      },
      setFeedOrderList: function (isSearch) {
        if (!isSearch) {
          this.search = _.cloneDeep(this.oldSearch);
        }

        var pagination = this.pagination;
        var pageSize = pagination.pageSize;
        var start = (pagination.currentPage - 1) * pageSize;

        var search = this.search;
        var postData = {
          start: start,
          length: pageSize,
          status: search.status,
          type: search.type,
          warehouse: search.warehouse,
        }
        var fields = ['materialName', 'model', 'color', 'providerName', 'factoryNo', 'orderId', 'companyOrderId', 'customerNo', 'providerNo'];
        SysUtil.setPostData(postData, search, fields);

        this.loading = true;
        var self = this;
        var user = this.user;
        ajaxRequest("order/getFeedOrderData", postData).then(function (res) {
          if (isSearch) {
            self.oldSearch = _.cloneDeep(self.search);
          }

          var approvalUserIds = res.approvalUserIds;
          if (SysUtil.isEmpty(approvalUserIds)) {
            approvalUserIds = [];
          }

          var feedOrderList = res.returnData;
          feedOrderList = _.map(feedOrderList, function (feed) {
            var order = new FeedOrderModel(feed, user, approvalUserIds);
            return order;
          });

          self.feedOrderList = feedOrderList;
          self.pagination.totalCount = res.iTotalRecords;
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      },
      resetSearch: function () {
        this.search = _.cloneDeep(this.oldSearch);
      },
      resetDropdown: function () {
        this.$refs['searchRef'].clearList();
      },
      searchData: function () {
        this.pagination.currentPage = 1;
        this.setFeedOrderList(true);
      },
      pageChange: function () {
        this.setFeedOrderList();
      },
      approvalFeed: function (feed) {
        var postData = {
          beanArray: JSON.stringify([{
            id: feed.detailId,
            approvalReason: "审批通过",
          }]),
          status: 1
        };

        this.loading = true;
        var self = this;
        ajaxRequest('approval/allApprovalFeedOrder', postData).then(function (msg) {
          self.loading = false;
          self.$alert(msg, '信息', {
            callback: function () {
              self.setFeedOrderList();
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      },
      retreatFeed: function (feed) {
        var self = this;
        this.$prompt('请输入退回原因', '退回原因', {
          inputValue: ''
        }).then(function (option) {
          var value = option.value;
          self.toRetreatFeed(feed, value.trim());
        }).catch(function () {});
      },
      toRetreatFeed: function (feed, reason) {
        if (reason === '') {
          reason = '弃用';
        }

        var postData = {
          beanArray: JSON.stringify([{
            id: feed.detailId,
            approvalReason: reason,
          }]),
          "status": 3
        };

        this.loading = true;
        var self = this;
        ajaxRequest('approval/allApprovalFeedOrder', postData).then(function (msg) {
          self.loading = false;
          self.$alert(msg, '信息', {
            callback: function () {
              self.setFeedOrderList();
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      },
      formClosed: function () {
        this.formDropdownOption = null;
        this.selectFeed = null;
      },
      getStatus: function (feed) {
        var status = feed.status;
        var statusList = this.statusList;
        var obj = _.find(statusList, {
          value: status
        });
        if (obj === undefined) {
          return '';
        }

        return obj.label;
      },
      lookMaterialUseCount: function (row) {
        if (row.relationCount > 0) {
          this.openMaterialUseCount = true;
          this.materialData = row;
        }
      }
    },
    components: {
      SysCommonSearch: SysCommonSearch,
      SearchBtnView: SearchBtnView,
      OrderAvailableCount: OrderAvailableCount
    },
    template: template
  };
}();